package pl.nazaweb.imedproplus.model.facade;

import java.util.List;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import pl.nazaweb.imedproplus.model.entity.DictionaryDefinition;
import pl.nazaweb.imedproplus.model.entity.DictionaryEntryDTO;

@Stateless
@LocalBean
public class DictionaryDefinitionFacade extends AbstractFacade<DictionaryDefinition> {

    @PersistenceContext(unitName = AbstractFacade.peristanceUnitName)
    private EntityManager entityManager;

    public DictionaryDefinitionFacade() {
        super(DictionaryDefinition.class);
    }

    @Override
    protected EntityManager getEntityManager() {
        return this.entityManager;
    }

    public DictionaryDefinition getDictionaryDefinitionById(Long id) {
        return (DictionaryDefinition) 
                this.getEntityManager()
                    .createNamedQuery("DictionaryDefinition.findById")
                    .setParameter("id", id)
                    .getSingleResult();
    }
    
    public List<DictionaryEntryDTO> getDictionaryEntries(String query){
        return ((Session) getEntityManager().getDelegate())
                    .createSQLQuery(query)
                    .setResultTransformer(Transformers.aliasToBean(DictionaryEntryDTO.class))
                    .list();
    }
}
